﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Configuration;
using System.Text.RegularExpressions;


namespace DingHeng.TransportationManagement.WebCommon.BaseUI
{
    public class BasePage : System.Web.UI.Page
    {

        /// <summary>
        /// 验证用户是否有权限打开页面
        /// </summary>
        /// <returns>返回验证结果</returns>
        protected virtual bool CheckPriovity()
        {
            return true;
        }



        protected override void OnInit(EventArgs e)
        {
            Login();

            bool isVal = this.CheckPriovity();

            if (isVal == false)
            {
                Response.Redirect("~/Pages/NoRight.htm");
            }

            base.OnInit(e);
        }

        /// <summary>
        /// 校验当前用户是否登录
        /// </summary>
        private void Login()
        {
            if (HttpContext.Current.Session[WebContext.SESSION_CURRSTAFF] == null)
            {
                Response.Redirect("/Login.aspx",true);

            }
        }



        #region 页面效果辅助函数

        /// <summary>
        /// 消息框
        /// </summary>
        /// <param name="message">消息内容</param>
        public void MessageBox(string message)
        {
            WriteClientScript("alert('" + message.Replace("\r\n", "\\r\\n").Replace("'", "\\'").Replace("\n", "\\n") + "');");
        }

        /// <summary>
        /// 消息框
        /// 并调用指定的javascript funciton
        /// </summary>
        /// <param name="message"></param>
        /// <param name="jsCallback"></param>
        public void MessageBox(string message, string jsCallback)
        {
            WriteClientScript("alert('" + message.Replace("\r\n", "\\r\\n").Replace("'", "\\'").Replace("\n", "\\n") + "');" + jsCallback);
        }

        /// <summary>
        /// 消息框并转到
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="url">跳转Url</param>
        public void MessageBoxAndRedirect(string message, string url)
        {
            Page.Response.Write(string.Format("<script language=javascript>alert('{0}');location.href='{1}';</script>", message, url));
        }

        /// <summary>
        /// 消息框并关闭
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="returnValue">返回值</param>
        public void MessageBoxAndClose(string message, string returnValue)
        {
            Page.Response.Write(string.Format("<script language=javascript>alert('{0}');window.returnValue={1};window.close();</script>", message, returnValue));
        }



        /// <summary>
        /// 向页面输出脚本
        /// </summary>
        /// <param name="script">脚本内容，不要包括script标签,请一定包括末尾分号</param>
        public void WriteClientScript(string script)
        {
            ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script language=javascript>" + script + "</script>");
        }


        #endregion
    }
}
